#include "filesystem.h"
#include "log.h"

#if defined(ANDROID)
extern AAssetManager* globalAassetMgr;
#endif

HFILE ufoFileOpen(const char* filename, char* mode)
{
	HFILE fp = 0;

#if defined(ANDROID)
	//ufoLog("ufoFileOpen", "ufoFileOpen %x filename = %s", fp, filename);

	fp = AAssetManager_open(globalAassetMgr, filename, AASSET_MODE_UNKNOWN);
#else
	fp = fopen(filename, mode);
#endif

	//ufoLog("ufoFileOpen", "ufoFileOpen %x filename = %s", fp, filename);

	return fp;
}

int ufoFileClose(HFILE file)
{
#if defined(ANDROID)
	AAsset_close(file);
	return file != 0;
#else
	return fclose(file);
#endif
}

int ufoFileRead(HFILE file, void* buffer, int size, int count)
{
#if defined(ANDROID)
	return AAsset_read(file, buffer, size*count);
#else
	return fread(buffer, size, count, file);
#endif
}

int ufoFileSeek(HFILE file, int offset, int origin)
{
#if defined(ANDROID)
	return AAsset_seek(file, offset, origin);
#else
	return fseek(file, offset, origin);
#endif
}

int ufoFileTell(HFILE file)
{
#if defined(ANDROID)
	return AAsset_getLength(file)- AAsset_getRemainingLength(file);
#else
	return ftell(file);
#endif
}